package Random._519_RandomFlipMatrix;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Solution {
    int[][] matrixs;
    int[][] origins;
    int length;
    int width;
    Random r;
    Map<Integer,Integer> maps;
    int total;
    public Solution(int n_rows, int n_cols) {
        this.length = n_rows;
        this.width = n_cols;
        this.matrixs = new int[n_rows][n_cols];
        this.origins = new int[n_rows][n_cols];
        this.r = new Random();
        this.maps = new HashMap<>();
        this.total = length * width;
    }

    public int[] flip() {
        int i = r.nextInt(total--);
        int pos = maps.getOrDefault(i, i);
        maps.put(i, maps.getOrDefault(total, total));
        return new int[]{pos / length, pos % width};
    }

    public void reset() {
        total = length * width;
    }
    //over time limited

    public int[] flip_0() {
        int res[] = new int[2];
        res[0] = r.nextInt(length);
        res[1] = r.nextInt(width);
        while(matrixs[res[0]][res[1]] == 1){
            res[0] = r.nextInt(length);
            res[1] = r.nextInt(width);
        }
        matrixs[res[0]][res[1]] = 1;
        return res;
    }

    public void reset_0() {
        for (int i = 0; i < length; i++) {
            matrixs[i] = Arrays.copyOf(origins[i], width);
        }
    }


}
